package pl.ptemich.tankwars.objects;

import java.text.NumberFormat;
import java.util.Random;

import org.newdawn.slick.Color;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;

public class Ball {

  /**
   * background 
   */
  private static Image ballImage;

  private static final float MAX_SPEED_X = 0.4f;
  
  private static final float MAX_SPEED_Y = 0.8f;
  
  private static final float BALL_RADIUS = 5.0f;
  
  private float fieldWidth;
  
  private float fieldHeight;  
  
  private float ballPosX;
  
  private float ballPosY;
  
  private float oldBallPosX = 10;
  
  private float oldBallPosY = 10;
  
  private float ballSpeedX;
  
  private float ballSpeedY;  
  
  NumberFormat nf = NumberFormat.getNumberInstance();
  
  public Ball(int fieldWidth, int fieldHeight) throws SlickException {
    
    nf.setMinimumFractionDigits(5);
    nf.setMaximumFractionDigits(5);
    nf.setMinimumIntegerDigits(4);
    
    this.fieldWidth = fieldWidth;
    this.fieldHeight = fieldHeight; 
    
    Random rand = new Random();
    ballPosX = Double.valueOf(fieldWidth * Math.sin(rand.nextDouble())).floatValue();
    ballPosY = Double.valueOf(fieldHeight * Math.sin(rand.nextDouble())).floatValue();
    
    ballSpeedX = Double.valueOf(MAX_SPEED_X * Math.sin(rand.nextDouble())).floatValue();
    ballSpeedY = Double.valueOf(MAX_SPEED_Y * Math.sin(rand.nextDouble())).floatValue();
    
    if (ballImage == null) {
      ballImage = new Image("resources/images/ball.png");
    }
  }
  
  public void render(Graphics graphics)
      throws SlickException {
    //ballImage.draw(ballPosX - BALL_RADIUS, ballPosY - BALL_RADIUS);
    graphics.setColor(Color.white);
    graphics.drawOval(ballPosX, ballPosY , BALL_RADIUS, BALL_RADIUS);
   // graphics.setColor(Color.gray);
    
    
    graphics.setColor(Color.white);
    graphics.drawOval(oldBallPosX, oldBallPosY , BALL_RADIUS-2, BALL_RADIUS-2);
    //graphics.setColor(Color.gray);
    
    //graphics.setLineWidth(BAll_BORDER);
    //graphics.fillOval(ballPosX + BAll_BORDER, ballPosY + BAll_BORDER , BALL_RADIUS - BAll_BORDER, BALL_RADIUS - BAll_BORDER);
  }  
  
  public void update(int delta) {
    oldBallPosY = ballPosY;
    oldBallPosX = ballPosX;
    
    float tmpXmov = (float) delta * ballSpeedX;
    float tmpYmov = (float) delta * ballSpeedY;
    
    
    ballPosX += tmpXmov; // (float) delta * ballSpeedX;
    ballPosY += tmpYmov; //  (float) delta * ballSpeedY;
    
    if (ballPosY + BALL_RADIUS > fieldHeight) {
      // top wall collision
      ballSpeedY *= -1;
      ballPosY = fieldHeight - BALL_RADIUS;
      System.out.println("VERTICAL REVERSE");
    }
    
    if (ballPosY - BALL_RADIUS < 0) {
      // bottom wall collision
      ballSpeedY *= -1;
      ballPosY = BALL_RADIUS;
      System.out.println("VERTICAL REVERSE");
    }    
    
    if (ballPosX - BALL_RADIUS < 0) {
      // left wall collision
      ballSpeedX *= -1;
      ballPosX = BALL_RADIUS;
      System.out.println("HORIZONTAL REVERSE");
    }
    
    if (ballPosX + BALL_RADIUS > fieldWidth) {
      // right wall collision
      ballSpeedX *= -1;
      ballPosX = fieldWidth - BALL_RADIUS; 
      System.out.println("HORIZONTAL REVERSE");
    }  
    System.out.println(delta + ":" + tmpXmov + " : " + tmpYmov + " : " + nf.format(ballPosX) + " : " + nf.format(ballPosY));
  }
  
}
